﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartsEAS_API.BLL.Interfaces.ICourseB;
using StartsEAS_API.DAL.CourseD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.CourseB
{
    public class CourseBLL : ICourseBLL
    {
        private readonly StarsEASContext _context;

        public CourseDAL _courseDal;

        public CourseBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _courseDal = new(_context);
        }
        public ResultUtils GetAllCourse()
        {
            return _courseDal.GetAllCourse();
        }

        public ResultUtils InsertCourse(JArray data)
        {
            List<Course> courseList = new List<Course>();
            foreach (var item in data) {
                string jsonStr = JsonConvert.SerializeObject(item);
                Course cou = JsonConvert.DeserializeObject<Course>(jsonStr);
                courseList.Add(cou);
            }
            return _courseDal.InsertCourse(courseList);
        }

        public ResultUtils UpdataCourse(Course data)
        {
            return _courseDal.UpdataCourse(data);
        }
    }
}
